package com.yyh.synctool.client.component;

import com.yyh.synctool.client.tool.ConfigKey;
import com.yyh.synctool.client.tool.CredentialConfig;
import com.yyh.synctool.client.tool.DBConfig;
import com.yyh.synctool.client.tool.TaskConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 工具设置
 * @author xuan
 * @date 2021/12/6
 */
@Component
public class Setting
{
    private DBConfig dbConfig;
    private TaskConfig taskConfig;
    private CredentialConfig credentialConfig;
    private boolean changed = false;

    @Autowired
    public Setting(DBConfig dbConfig, TaskConfig taskConfig, CredentialConfig credentialConfig) {
        this.dbConfig = dbConfig;
        this.taskConfig = taskConfig;
        this.credentialConfig = credentialConfig;
    }

    public boolean isChanged() {
        return changed;
    }

    /************************数据库连接配置*************************/
    public void saveDBConfig() {
        dbConfig.store();
        this.changed = true;
    }

    public String getType() {
        return dbConfig.getProperty(ConfigKey.DB_TYPE);
    }

    public String getHost() {
        return dbConfig.getProperty(ConfigKey.DB_HOST);
    }

    public String getPort() {
        return dbConfig.getProperty(ConfigKey.DB_PORT);
    }

    public String getUsername() {
        return dbConfig.getProperty(ConfigKey.DB_USERNAME);
    }

    public String getPassword() {
        return dbConfig.getProperty(ConfigKey.DB_PASSWORD);
    }

    public String getDatabase() {
        return dbConfig.getProperty(ConfigKey.DB_DATABASE);
    }

    public String getUrl() {
        return dbConfig.getProperty(ConfigKey.DB_URL);
    }

    public Setting setType(String type) {
        dbConfig.setProperty(ConfigKey.DB_TYPE, type);
        return this;
    }

    public Setting setHost(String host) {
        dbConfig.setProperty(ConfigKey.DB_HOST, host);
        return this;
    }

    public Setting setPort(String port) {
        dbConfig.setProperty(ConfigKey.DB_PORT, port);
        return this;
    }

    public Setting setUsername(String username) {
        dbConfig.setProperty(ConfigKey.DB_USERNAME, username);
        return this;
    }

    public Setting setPassword(String password) {
        dbConfig.setProperty(ConfigKey.DB_PASSWORD, password);
        return this;
    }

    public Setting setDatabase(String database) {
        dbConfig.setProperty(ConfigKey.DB_DATABASE, database);
        return this;
    }

    public Setting setUrl(String url) {
        dbConfig.setProperty(ConfigKey.DB_URL, url);
        return this;
    }

    /************************任务配置*************************/
    public void saveTaskConfig() {
        taskConfig.store();
        this.changed = true;
    }

    public String getTaskSql(String key) {
        return taskConfig.getProperty(key);
    }

    public Setting setTaskSql(String key, String taskSql) {
        taskConfig.setProperty(key, taskSql);
        return this;
    }

    /************************任务配置*************************/
    public void saveCredentialConfig() {
        credentialConfig.store();
    }

    public String getGsid() {
        return credentialConfig.getProperty(ConfigKey.GSID);
    }

    public String getCredential() {
        return credentialConfig.getProperty(ConfigKey.CREDENTIAL);
    }

    public Setting setGsid(String gsid) {
        credentialConfig.setProperty(ConfigKey.GSID, gsid);
        return this;
    }

    public Setting setCredential(String credential) {
        credentialConfig.setProperty(ConfigKey.CREDENTIAL, credential);
        return this;
    }

}
